package com.xauto.pioneer.appuser.activity.mine;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.xauto.pioneer.appuser.R;
import com.xauto.pioneer.appuser.activity.BaseActivity;
import com.xauto.pioneer.appuser.activity.mine.user.OrderOtherListActivity;
import com.xauto.pioneer.appuser.activity.mine.user.RefuelListActivity;
import com.xauto.pioneer.appuser.activity.takeCar.OrderCostActivity;
import com.xauto.pioneer.appuser.adapter.ExpenseAdapter;
import com.xauto.pioneer.appuser.http.BaseLHttpHandler;
import com.xauto.pioneer.appuser.http.LHttpLib;
import com.xauto.pioneer.appuser.model.AdditionalInfo;
import com.xauto.pioneer.appuser.model.BillChargeInfo;
import com.xauto.pioneer.appuser.model.JSONStatus;
import com.xauto.pioneer.appuser.model.OrderDetailInfo;
import com.xauto.pioneer.appuser.model.Payment;
import com.xauto.pioneer.appuser.model.RefuelInfo;
import com.xauto.pioneer.appuser.model.VoucherInfo;
import com.xauto.pioneer.appuser.util.ListViewUtil;
import com.xauto.pioneer.appuser.util.StringUtils;
import com.xauto.pioneer.appuser.util.TimeUtil;
import com.xauto.pioneer.appuser.view.TopBar;

import butterknife.Bind;
import butterknife.OnClick;
import de.greenrobot.event.EventBus;

/**
 * Created by Ellis.Li on 2017/5/25.
 */

public class OrderDetailActivity extends BaseActivity {
    @Bind(R.id.topBar)
    TopBar topBar;
    @Bind(R.id.tvCarSeries)
    TextView tvCarSeries;
    @Bind(R.id.tvCarPlate)
    TextView tvCarPlate;
    @Bind(R.id.tvOrderNo)
    TextView tvOrderNo;
    @Bind(R.id.tvOrderSubmitDate)
    TextView tvOrderSubmitDate;
    @Bind(R.id.tvBillDate)
    TextView tvBillDate;
    @Bind(R.id.tvReturnCarDate)
    TextView tvReturnCarDate;
    @Bind(R.id.TestTvKM)
    TextView TestTvKM;
    @Bind(R.id.tvKM)
    TextView tvKM;
    @Bind(R.id.departure_expense)
    TextView departure_expense;
    @Bind(R.id.enter_expense)
    TextView enter_expense;
    @Bind(R.id.tvKMMoney)
    TextView tvKMMoney;
    @Bind(R.id.tvUseTime)
    TextView tvUseTime;
    @Bind(R.id.tvUseTimeMoney)
    TextView tvUseTimeMoney;
    @Bind(R.id.tvEnergy)
    TextView tvEnergy;
    @Bind(R.id.tvEnergyMoney)
    TextView tvEnergyMoney;
    @Bind(R.id.tvSubtotalMoney)
    TextView tvSubtotalMoney;
    @Bind(R.id.tvPreferentialMoney)
    TextView tvPreferentialMoney;
    @Bind(R.id.tvAllMoney)
    TextView tvAllMoney;
    @Bind(R.id.tvOrderStatus)
    TextView tvOrderStatus;
    @Bind(R.id.tvOrderRemark)
    TextView tvOrderRemark;
    @Bind(R.id.tvOrderPayMethod)
    TextView tvOrderPayMethod;
    @Bind(R.id.tvRefuelMoney)
    TextView tvRefuelMoney;
    @Bind(R.id.tvOtherMoney)
    TextView tvOtherMoney;
    @Bind(R.id.tvPay)
    TextView tvPay;
    @Bind(R.id.pay_method_linear)
    LinearLayout payMethodlinear;
    @Bind(R.id.preferentialMoneyLinear)
    LinearLayout preferentialMoneyLinear;
    @Bind(R.id.refuel_parent)
    LinearLayout refuelParent;
    @Bind(R.id.parent_view)
    LinearLayout parentView;
    @Bind(R.id.detailList)
    ListView detailList;
    ExpenseAdapter expenseAdapter;
    OrderDetailInfo orderDetailInfo;
    String car_order_no = "";
    public static void startAction(Context context, String car_order_no) {
        Intent intent = new Intent(context, OrderDetailActivity.class);
        intent.putExtra("car_order_no", car_order_no);
        context.startActivity(intent);
    }

    @Override
    protected int getContentView() {
        return R.layout.ac_order_detail;
    }

    @Override
    protected void initView() {
        topBar.showView(TopBar.LEFT_IMAGE_BTN, TopBar.TITLE);
        topBar.setImage(TopBar.LEFT_IMAGE_BTN, R.mipmap.ic_common_left);
        topBar.setText(TopBar.TITLE, "订单详情");
        topBar.setLeftOnClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        expenseAdapter = new ExpenseAdapter(OrderDetailActivity.this,R.layout.item_expense);
        detailList.setAdapter(expenseAdapter);
    }

    @Override
    protected void initData() {
        EventBus.getDefault().register(this);
        car_order_no =  getIntent().getStringExtra("car_order_no");
    }

    @Override
    protected void onStart() {
        super.onStart();
        loadOrderDetail();
    }

    @OnClick(R.id.tvPay)
    public void onViewClicked() {
        //跳转至订单结算界面
        OrderCostActivity.startAction(mContext,orderDetailInfo.car_order_no);
    }
    /**
     *加载订单详情信息
     */
    private void loadOrderDetail() {
        new LHttpLib().getCarOrderDetailInfo(mContext, memberId,car_order_no, new BaseLHttpHandler(mContext, true, true) {
            @Override
            public void onSuccess(JSONStatus jsonStatus) {
                super.onSuccess(jsonStatus);
                orderDetailInfo = new OrderDetailInfo().parse(jsonStatus.data.optJSONObject("car_order_info"));
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        showOrderData();
                    }
                });
            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                super.onFailure(t, errorNo, strMsg);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        parentView.setVisibility(View.VISIBLE);
                    }
                });
            }
        });
    }

    /**
     * 显示相关数据
     */
    private void showOrderData(){
        parentView.setVisibility(View.VISIBLE);
        expenseAdapter.getDataList().clear();
        expenseAdapter.getDataList().addAll(orderDetailInfo.order_total_list);
        expenseAdapter.notifyDataSetChanged();
        ListViewUtil.setListViewHeightBasedOnChildren(detailList);
        tvCarSeries.setText(orderDetailInfo.car_info.car_series_name);
        tvCarPlate.setText(orderDetailInfo.car_info.plate_no);
        tvOrderNo.setText(getString(R.string.order_no,orderDetailInfo.car_order_no));
        if (!StringUtils.isEmpty(orderDetailInfo.order_start_time))
        tvOrderSubmitDate.setText(TimeUtil.getStrTime(Long.parseLong(orderDetailInfo.order_start_time),"yyyy-MM-dd HH:mm"));
        if (!StringUtils.isEmpty(orderDetailInfo.order_charge_start_time))
        tvBillDate.setText(TimeUtil.getStrTime(Long.parseLong(orderDetailInfo.order_charge_start_time),"yyyy-MM-dd HH:mm"));
        if (!StringUtils.isEmpty(orderDetailInfo.order_charge_end_time))
        tvReturnCarDate.setText(TimeUtil.getStrTime(Long.parseLong(orderDetailInfo.order_charge_end_time),"yyyy-MM-dd HH:mm"));
        BillChargeInfo billChargeInfo = orderDetailInfo.billing_charge_info;
        if (billChargeInfo != null){
            TestTvKM.setText( orderDetailInfo.test_total_kilometer+"公里");
            tvKM.setText( billChargeInfo.kilometre+"公里");
            tvKMMoney.setText( billChargeInfo.total_kilometre+"元");
            tvUseTime.setText( billChargeInfo.run_time+"分钟");
            tvUseTimeMoney.setText( billChargeInfo.total_run_time+"元");
            tvEnergy.setText( billChargeInfo.energy+"度");
            tvEnergyMoney.setText( billChargeInfo.total_energy+"元");
             /*站点取车还车费用*/
            enter_expense.setText(billChargeInfo.enter_expense+"元");
            departure_expense.setText(billChargeInfo.departure_expense+"元");
        }
        //优惠
        VoucherInfo voucherInfo = orderDetailInfo.voucher_info;
        if (voucherInfo != null){
            preferentialMoneyLinear.setVisibility(View.VISIBLE);
            tvPreferentialMoney.setText(voucherInfo.discount+"元");
        }else {
            preferentialMoneyLinear.setVisibility(View.GONE);
        }
        tvAllMoney.setText(orderDetailInfo.total_amount_format);
        tvOrderStatus.setText(orderDetailInfo.car_order_status_name);
        tvOrderRemark.setText(orderDetailInfo.car_order_remark);
        //加油信息
        if (orderDetailInfo.refuel_info != null){
            RefuelInfo refuelInfo = orderDetailInfo.refuel_info;
            refuelParent.setVisibility(View.VISIBLE);
            tvRefuelMoney.setText(StringUtils.isEmpty(refuelInfo.getRefuel_total_amount_format()) ? "0元" : orderDetailInfo.refuel_info.getRefuel_total_amount_format());
        }else {
            refuelParent.setVisibility(View.GONE);
        }
        Payment paymentInfo = orderDetailInfo.payment_info;
        if (paymentInfo != null){
            tvOrderPayMethod.setText(paymentInfo.payment_name);
            payMethodlinear.setVisibility(View.VISIBLE);
        }else {
            payMethodlinear.setVisibility(View.GONE);

        }
        //附加费
        if (orderDetailInfo.additional_info != null){
            AdditionalInfo additionalInfo = orderDetailInfo.additional_info;
            tvOtherMoney.setVisibility(View.VISIBLE);
            tvOtherMoney.setText(StringUtils.isEmpty(additionalInfo.getAdditional_total_amount_format()) ? "0元" : orderDetailInfo.additional_info.getAdditional_total_amount_format());
        }else {
           // refuelParent.setVisibility(View.GONE);
        }
        // TODO: 2017/5/25  小计未做处理

//        11	预约进行中	预约进行中	0	预约中
//        12	预约已取消	预约已取消	1	已取消
//        13	预约取消违约待支付	预约取消违约待支付	1	待支付
//        14	预约取消违约已支付	预约取消违约已支付	1	已支付
//        21	正常使用进行中	正常使用进行中	0	使用中
//        22	正常使用完成待支付	正常使用完成待支付	1	待支付
//        23	正常使用完成已支付	正常使用完成已支付	1	已支付
//        31	异常使用关闭待支付	异常使用关闭待支付	1	待支付
//        32	异常使用关闭已支付	异常使用关闭已支付	1	已支付
//        41	后台关闭	后台关闭	1	已关闭
//        24	正常使用完成等待中	正常使用完成等待中	1	计算中
        //支付按钮做判断
        Log.e("支付结果",orderDetailInfo.car_order_status_id);
        if ("13".equals(orderDetailInfo.car_order_status_id) || "22".equals(orderDetailInfo.car_order_status_id) ||
                "31".equals(orderDetailInfo.car_order_status_id)||"24".equals(orderDetailInfo.car_order_status_id)){
            tvPay.setVisibility(View.VISIBLE);
        }else {
            tvPay.setVisibility(View.GONE);
        }
    }
    @OnClick({R.id.tvRefuelMoney, R.id.tvOtherMoney})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tvRefuelMoney:
                RefuelListActivity.startAction(mContext,orderDetailInfo.car_order_no);
                break;
            case R.id.tvOtherMoney:
                OrderOtherListActivity.startAction(mContext,orderDetailInfo.car_order_no);
                break;
        }
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
    /**
     * 接受支付成功的通知信息
     * @param paymentCode
     */
    public void onEventMainThread(String paymentCode){
        switch (paymentCode){
            case "success":
               finish();
        }
    }
}
